GBML manual / Stacks

stack_push

Pushes value onto the top of target, a stack - a container with only ONE end anyone may ever touch. Returns true if it fit, false if target was already full (nothing is pushed - target never crashes or corrupts, it just stops growing).

Syntax

stack_push(target, value) -> bool

Arguments

NameType
targetstack
valueelement

Returns

bool

Description

Pushes value onto the top of target, a stack<T> - a container with only ONE end anyone may ever touch. Returns true if it fit, false if target was already full (nothing is pushed - target never crashes or corrupts, it just stops growing).

target is a real fixed-size vector that holds up to N items - N is target's own declared capacity (/// @capacity, default 8 if untagged, same rule every other container on this manual uses). If the return value is ignored, target simply stops accepting new items until something is popped; check it only when the game needs to react to "no room left."

stack<T> has no [i] indexing - unlike array<T>/list<T>/grid<T>, the top is the only item you can ever reach directly. Declare the variable list<T> instead if you need to look inside it by index or search it.

Example

/// @type {stack<int>}
var history = [];
stack_push(history, 1);
var added = stack_push(history, 2); // true - there was room
A real capture - the stack right after stack_push(history, 1)A real capture - the stack right after stack_push(history, 1)
A real capture - the stack right after stack_push(history, 1)
The same stack after stack_push(history, 2) too - both items now show, top lastThe same stack after stack_push(history, 2) too - both items now show, top last
The same stack after stack_push(history, 2) too - both items now show, top last

On the GBA

target is a real fixed-size vector of T items, up to N - N is target's own declared capacity (`/// @capacity`, default 8 if untagged). This checks target.full() before ever calling push_back, the identical safety check list_add makes - an author who never checks the return value still cannot crash their own game by pushing one item too many. O(1).

See also: stack_popstack_peekstack_lengthstack_clearstack_emptyData containers